package model

import (
	"encoding/json"
	"sync"
)

const (
	NodeTypeStart         = "start"
	NodeTypeApprove       = "approve"
	NodeTypeCondition     = "condition"
	NodeTypeCallback      = "callback"
	NodeTypeEnd           = "end"
	StatusRunning         = "running"
	StatusCompleted       = "completed"
	StatusPendingApproval = "pending_approval"
)

// JSON配置结构体
type Workflow struct {
	BaseModel
	Name        string          `json:"name" gorm:"type:varchar(128);column:name"`
	Description string          `json:"description" gorm:"type:varchar(128);column:description"`
	FormConfig  json.RawMessage `json:"formConfig" gorm:"type:json;column:form_config"`
	FlowConfig  json.RawMessage `json:"flowConfig" gorm:"type:json;column:flow_config"`
	Metadata    json.RawMessage `json:"metadata" gorm:"type:json;column:metadata"`
}

type NodeAssignee struct {
	DeletedModel
	Type               int8   `json:"type" gorm:"type:tinyint(4);column:type"`
	Name               string `json:"name" gorm:"type:varchar(128);column:name"`
	TargetId           uint64 `json:"targetId" gorm:"type:bigint(20);column:target_id"`
	NodeId             uint64 `json:"nodeId" gorm:"type:bigint(20);column:node_id"`
	WorkflowInstanceId uint64 `json:"workflowInstanceId" gorm:"type:bigint(20);column:workflow_instance_id"`
}

type NodeOperator struct {
	BaseModel
	NodeId                 uint64 `json:"nodeId" gorm:"type:bigint(20);column:node_id"`
	NodeName               string `json:"nodeName" gorm:"type:varchar(128);column:node_name"`
	WorkflowInstanceId     uint64 `json:"workflowInstanceId" gorm:"type:bigint(20);column:workflow_instance_id"`
	Operator               string `json:"operator" gorm:"type:varchar(128);column:operator"`
	OperatorId             uint64 `json:"operatorId" gorm:"type:bigint(20);column:operator_id"`
	Action                 int    `json:"action" gorm:"type:tinyint(4);column:action"`
	WorkflowInstanceStatus string `json:"workflowInstanceStatus" gorm:"type:varchar(128);column:workflow_instance_status"`
}

type Node struct {
	BaseModel
	WorkflowInstanceId uint64          `json:"workflowInstanceId" gorm:"type:bigint(20);column:workflow_instance_id"`
	NodeName           string          `json:"nodeName" gorm:"type:varchar(128);column:node_name"`
	Type               int             `json:"type" gorm:"type:tinyint(4);column:type"`
	ExamineMode        int             `json:"examineMode" gorm:"type:tinyint(4);column:examine_mode"`
	ChildNode          json.RawMessage `json:"childNode" gorm:"type:json;column:child_node"`
	ConditionNodes     json.RawMessage `json:"conditionNodes" gorm:"type:json;column:condition_nodes"`
	ParentNodes        json.RawMessage `json:"parentNodes" gorm:"type:json;column:parent_nodes"`
	Condition          string          `json:"condition" gorm:"type:varchar(2048);column:condition"`
	Status             string          `json:"status" gorm:"type:varchar(128);column:status"` // 0:创建 1:通过 2:不通过 3:审批中(会签只有部分人完成时)
}

type WorkflowInstance struct {
	BaseModel
	WorkflowId      uint64          `json:"workflowId" gorm:"type:bigint(20);column:workflow_id"`
	WorkflowName    string          `json:"workflowName" gorm:"type:varchar(128);column:workflow_name"`
	CurrentNodeId   uint64          `json:"currentNodeId" gorm:"type:bigint(20);column:current_node_id"`
	CurrentNodeName string          `json:"currentNodeName" gorm:"type:varchar(128);column:current_node_name"`
	FormConfig      json.RawMessage `json:"formConfig" gorm:"type:json;column:form_config"`
	Variables       json.RawMessage `json:"variables" gorm:"type:json;column:variables"`
	Status          string          `json:"status" gorm:"type:varchar(128);column:status"`   // running, completed, pending_approval
	Priority        int             `json:"priority" gorm:"type:tinyint(4);column:priority"` // 紧急程度由低到高方便后续扩展, 1:紧急 0:普通 -1:低
}

type WorkflowEngine struct {
	workflows     map[string]*Workflow
	callbackFuncs map[string]func(map[string]interface{}) (map[string]interface{}, error)
	instances     map[string]*WorkflowInstance
	approvalQueue chan string
	mu            sync.RWMutex
}

type NodeActionHistory struct {
	BaseModel
	WorkflowInstanceId uint64          `json:"workflowInstanceId" gorm:"type:bigint(20);column:workflow_instance_id"`
	NodeId             uint64          `json:"nodeId" gorm:"type:bigint(20);column:node_id"`
	NodeName           string          `json:"nodeName" gorm:"type:varchar(128);column:node_name"`
	NodeType           int             `json:"nodeType" gorm:"type:tinyint(4);column:node_type"`
	Type               int             `json:"type" gorm:"type:tinyint(4);column:type"`
	ExamineMode        int             `json:"examineMode" gorm:"type:tinyint(4);column:examine_mode"`
	ActionInfo         json.RawMessage `json:"actionInfo" gorm:"type:json;column:action_info"`
	Comment            string          `json:"comment" gorm:"type:varchar(128);column:comment"`
	Status             string          `json:"status" gorm:"-"`
}
